import random
from time import sleep
from page import l_config_front_end as l_conf
from selenium.webdriver.common.by import By
from base.base import Base


class PageFrontEnd(Base):
    # 添加cookie
    def page_add_cookies(self):
        self.set_cookie("../data/l_front_end_cookies.json")

    # 回到首页
    def page_back_home(self):
        self.driver.find_element(By.LINK_TEXT, '首页').click()

    # 点击搜索
    def page_click_search(self, good_name):
        self.input(l_conf.input_text, good_name)
        self.click(l_conf.input_brn)

    # 点击随机一个商品
    def page_click_goods(self):
        self.driver.implicitly_wait(1)
        goods = self.driver.find_elements(By.XPATH,'/html/body/div[4]/div/ul/li')
        goods[random.randint(0, len(goods) - 1)].click()


    # 操作最新页面
    def page_go_new(self):
        self.driver.close()
        # 等待新窗口打开
        sleep(2)  # 可以根据实际情况调整等待时间
        # 获取当前所有窗口的句柄
        window_handles = self.driver.window_handles
        # 切换到新打开的标签页 (最新的窗口句柄是最后一个)
        self.driver.switch_to.window(window_handles[-1])


    # 商品样式和数量选择
    def page_click_select(self):
        self.driver.implicitly_wait(1)
        try:

            select_one = self.driver.find_elements(By.XPATH,'/html/body/div[4]/div[2]/div[2]/div[2]/dl/dd/div[2]/div[3]/form/div[1]/div[1]/ul/li[not(contains(concat(" ", normalize-space(@class), " "),  "sku-items-disabled"))]')
            select_one[random.randint(0, len(select_one)-1)].click()
            sleep(1)
        except:
            pass
        try:

            select_tow = self.driver.find_elements(By.XPATH,'/html/body/div[4]/div[2]/div[2]/div[2]/dl/dd/div[2]/div[3]/form/div[1]/div[2]/ul/li[not(contains(concat(" ", normalize-space(@class), " "),  "sku-items-disabled"))]')
            select_tow[random.randint(0, len(select_tow)-1)].click()
            sleep(1)
        except:
            pass
        try:

            select_three = self.driver.find_elements(By.XPATH,'/html/body/div[4]/div[2]/div[2]/div[2]/dl/dd/div[2]/div[3]/form/div[1]/div[3]/ul/li[not(contains(concat(" ", normalize-space(@class), " "),  "sku-items-disabled"))]')
            select_three[random.randint(0, len(select_three)-1)].click()
            sleep(1)
        except:
            pass

    # 购买数量
    def page_buy_num(self):
        self.click(l_conf.good_add)
        sleep(1)
        self.click(l_conf.good_min)

    # 立即购买
    def page_buy_good(self):
        self.click(l_conf.good_buy)

    # 加入购物车
    def page_add_shopping_cart(self):
        self.click(l_conf.into_shopping_cart)

    # 收藏
    def page_good_collect(self):
        self.click(l_conf.good_collect)

    # 选择地址
    def page_select_arsa(self):
        self.click(l_conf.select_arsa1)
        self.click(l_conf.select_arsa2)

    # 现金支付
    def page_good_payment(self):
        self.click(l_conf.good_payment)

    # 提交订单
    def page_submit_order_for_good(self):
        self.click(l_conf.submit_order_for_good)

    # 判断元素是否存在并获取报错信息
    def page_if_exist(self):
        try:
            self.find_element(l_conf.search_error_text, timeout=2)
            # 找到元素
            return self.get_text(l_conf.search_error_text)
        except:
            # 没找到元素
            return ''

    # 获取报错信息
    def page_get_error_info(self):
        return self.get_text(l_conf.search_error_text)

    # 搜索操作
    def page_search_text(self, search_value):
        self.page_add_cookies()
        self.page_back_home()
        self.page_click_search(search_value)



if __name__ == '__main__':
    p = PageFrontEnd()
    p.page_search_text('睡衣')
    p.page_click_goods()
    p.page_go_new()
    # p.page_good_collect()
    p.page_click_select()
    p.page_buy_good()
    p.page_select_arsa()
    p.page_good_payment()
    sleep(1)
    p.page_submit_order_for_good()
    # print(p.page_if_exist())
    # p.page_back_home()
    sleep(3)
